﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CalibrationLibrary
{
    public class MCMC
    {
        static Random R = new Random();

        static bool accepted;
        //public static bool Accepted { get { return accepted; } }

        public static double fractionofdomain;

        
        public delegate double GetProbability(Parameters parameters);

        public static void RunMCMC(Parameters parameters, GetProbability getmodelprobability, int Iterations, double JumpSize)
        {
            fractionofdomain = JumpSize;

            double LogP_last = double.MinValue;

            for (int i = 0; i < Iterations; i++)
            {
                double logp = getmodelprobability(parameters);

                double LogMetropolisAlpha = logp - LogP_last;

                double MetropolisAlpha = Math.Exp(LogMetropolisAlpha);

                accepted = LogMetropolisAlpha > Math.Log(R.NextDouble());

                //System.Console.WriteLine("A\t" + Math.Round( parameters["A"].RunningValue,2) + "\taccepted = " + accepted);
                if (accepted)
                {
                    parameters.AcceptRunningValues();
                    parameters.Jump(fractionofdomain);
                }
                else
                {
                    parameters.UseLastAcceptedValues();
                }

                LogP_last = logp;
            }
            System.Console.WriteLine("ready");
            //System.Console.ReadLine();
        }

    }
}
